package com.hualan.dao;

import com.hualan.Util.DBUtils;
import com.hualan.bean.PanghuShopGoods;
import com.hualan.dao.PanghuGoodsLike;
//import com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl;

import javax.management.Query;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PanghuGoodsLikeImpl implements PanghuGoodsLike {
    @Override
    public List<PanghuShopGoods> selectShopGoodLike(String goodsName,String shopName,String startTime, String endTime,String goodsStatus, String pageNum, String pageSize) {
        // 判断查询条件
        String condition = " WHERE 1=1";
        // 用列表接收有效参数
        List<String> params = new ArrayList<>();

        if(goodsName != null && !"".equals(goodsName)){
            condition += " and goods_name like ?";
            params.add("%" + goodsName + "%");
        }
        if(shopName != null && !"".equals(shopName)){
            condition += " and goods_shop_name like ?";
            params.add("%" + shopName + "%");
        }
        if (startTime != null && !"".equals(startTime)){
            condition += "  and add_time>=?";
            params.add(startTime);
        }
        if (endTime != null && !"".equals(endTime)){
            condition += " and add_time<=?";
            params.add(endTime);
        }

        condition += " limit " + pageNum + ", " + pageSize;
        String sql = "select * from panghu_shop_goods" + condition;
        System.out.println("完整的查询条件是："+sql);
        System.out.println("查询前："+ params);

        List<PanghuShopGoods> selectgoods = DBUtils.query(sql,PanghuShopGoods.class, params.toArray());
        System.out.println("查询到的条数是：" + selectgoods.size());
        return selectgoods;

    }

    @Override
    public long selectGoodsTotal() {
        Map<String, Object> map = DBUtils.queryOne("select count(goods_id) as count from panghu_shop_goods");
        Long count  = (Long) map.get("count");
        return count;
    }
}
